package cn.org.atool.generator.javafile.template;

import cn.org.atool.generator.database.model.TableSetter;
import com.squareup.javapoet.*;

import javax.lang.model.element.Modifier;

import static com.squareup.javapoet.ClassName.get;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author by lishupeng
 * @date 2022/3/31 22:09
 */
public class ServiceInterfaceFile extends AbstractTemplateFile {

    public ServiceInterfaceFile(TableSetter table) {
        super(table);
        this.packageName = getPackage(table);
        this.klassName = getClass(table);
    }

    public static String getPackage(TableSetter table) {
        return table.gc().getBasePackage() + ".intf";
    }

    public static String getClass(TableSetter table) {
        return table.getEntityPrefix() + "Service";
    }

    public static ClassName getClassName(TableSetter table) {
        return ClassName.get(getPackage(table), getClass(table));
    }


    /**
     * 文件构造
     *
     * @param spec
     */
    @Override
    protected void build(TypeSpec.Builder spec) {
        String basePackage = table.gc().getBasePackage();
        String entityPrefix = table.getEntityPrefix();
        //继承父接口
        if (table.getSuperService() != null) {
            spec.addSuperinterface(parameterizedType(ClassName.get(table.getSuperService()), EntityFile.entityName(table)));
        }

        //方法-query
        spec.addMethod(MethodSpec.methodBuilder("query")
                .addModifiers(Modifier.DEFAULT, Modifier.PUBLIC)
                .returns(ClassName.get(basePackage + ".wrapper", entityPrefix + "Query"))
                .addStatement("return new " + entityPrefix + "Query().where.apply(EntityUtil.is_deleted, SqlOp.EQ, 0).end()")
                .build());
        //方法-updater
        spec.addMethod(MethodSpec.methodBuilder("updater")
                .addModifiers(Modifier.DEFAULT, Modifier.PUBLIC)
                .returns(ClassName.get(basePackage + ".wrapper", entityPrefix + "Update"))
                .addStatement("return new " + entityPrefix + "Update()")
                .build());


        ClassName pageDTOClz = get("com.dminfo.hms.common.domain.dto", "PageDTO");
        ClassName pageVOClz = get("com.dminfo.hms.common.domain.vo", "PageVO");
        ClassName entityClz = get(table.gc().getBasePackage() + ".entity", table.getEntityPrefix() + "Entity");


        spec
                .addMethod(MethodSpec.methodBuilder("page")
                        .addJavadoc("分页")
                        .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                        .addParameter(ParameterSpec.builder(pageDTOClz, "dto").build())
                        .returns(ParameterizedTypeName.get(pageVOClz, entityClz))
                        .build())
                .addMethod(MethodSpec.methodBuilder("list")
                        .addJavadoc("列表")
                        .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                        .addParameter(ParameterSpec.builder(entityClz, "entity").build())
                        .returns(ParameterizedTypeName.get(get("java.util","List"), entityClz))
                        .build())
                .addMethod(MethodSpec.methodBuilder("getById")
                        .addJavadoc("查询")
                        .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                        .addParameter(ParameterSpec.builder(get("java.lang","Long"), "id").build())
                        .returns(entityClz)
                        .build())
                .addMethod(MethodSpec.methodBuilder("save")
                        .addJavadoc("保存")
                        .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                        .addParameter(ParameterSpec.builder(entityClz, "entity").build())
                        .build())
                .addMethod(MethodSpec.methodBuilder("update")
                        .addJavadoc("更新")
                        .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                        .addParameter(ParameterSpec.builder(entityClz, "entity").build())
                        .build())
                .addMethod(MethodSpec.methodBuilder("delete")
                        .addJavadoc("删除")
                        .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                        .addParameter(ParameterSpec.builder(get("java.lang","Long"), "id").build())
                        .build());


        spec.addJavadoc("$T: 服务类接口\n", super.className());
    }

    /**
     * 是否接口类
     *
     * @return ignore
     */
    @Override
    protected boolean isInterface() {
        return true;
    }
}
